﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;
using TMDT.BusinessObjects;

namespace TMDT.Web.UI.Controls
{
    public partial class LoaiTaiKhoanControl : System.Web.UI.UserControl
    {
        public static Guid IDLoaiTaiKhoan;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            mtvView.ActiveViewIndex = 0;

            txtTenLoaiTaiKhoanInsert.Text = "";
            txtQuyenInsert.Text = "";

            var service = new LoaiTaiKhoanManager();
            rptLoaiTaiKhoan.DataSource = service.GetLoaiTaiKhoan();
            rptLoaiTaiKhoan.DataBind();
        }

        protected void lbnInsert_Click(object sender, EventArgs e)
        {
            var entity = new LoaiTaiKhoan
            {
                IDLoaiTaiKhoan = Guid.NewGuid(),
                Quyen = int.Parse(txtQuyenInsert.Text),
                TenLoaiTaiKhoan = txtTenLoaiTaiKhoanInsert.Text
            };
            var service = new LoaiTaiKhoanManager();
            service.Add(entity);
            LoadData();
        }

        protected void lbnUpdate_Click(object sender, EventArgs e)
        {
            var service = new LoaiTaiKhoanManager();
            var entity = service.GetLoaiTaiKhoan(IDLoaiTaiKhoan);
            entity.TenLoaiTaiKhoan = txtTenLoaiTaiKhoanUpdate.Text;
            entity.Quyen = int.Parse(txtQuyenUpdate.Text);

            service.Update(entity);
            LoadData();
        }

        protected void lbnCancel_Click(object sender, EventArgs e)
        {
            mtvView.ActiveViewIndex = 0;
        }

        protected void ibnDelete_Click(object sender, ImageClickEventArgs e)
        {
            var id = (((ImageButton) sender).CommandArgument);
            var service = new LoaiTaiKhoanManager();
            service.Remove(id);
            LoadData();
        }

        protected void ibnEdit_Click(object sender, ImageClickEventArgs e)
        {
            var id = ((ImageButton) sender).CommandArgument;
            var service = new LoaiTaiKhoanManager();
            var entity = service.GetLoaiTaiKhoan(id);

            txtQuyenUpdate.Text = entity.Quyen.ToString();
            txtTenLoaiTaiKhoanUpdate.Text = entity.TenLoaiTaiKhoan;
            IDLoaiTaiKhoan = entity.IDLoaiTaiKhoan;

            mtvView.ActiveViewIndex = 1;
        }
    }
}